package com.jiangruan.project.service;

import com.alibaba.fastjson.JSONObject;
import com.jiangruan.project.Constants;
import com.jiangruan.project.request.HttpUtils;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:liangdu
 * @email:1487001474@qq.com
 * @desc:
 * @datetime: 2021/12/19 19:57
 */
public class GetBlockCount {
    public static String getblockcout(){
        String auth = Constants.BITCOIN_RPC_USER + ":" + Constants.BITCOIN_RPC_PWD;
        //base64编码
        Base64.Encoder encoder = Base64.getEncoder();
        String authBase64 =  encoder.encodeToString(auth.getBytes());

        //准备请求头
        Map<String,String> header = new HashMap<>();
        header.put("ContentType","application/json");
        header.put("Authorization","Basic "+ authBase64);

        //准备请求数据
        Map<String,Object> body = new HashMap<>();
        body.put("id",1001);
        body.put("method","getblockcount");
        body.put("params",null);
        body.put("json_rpc","2.0");
        byte[] bodyBytes =  JSONObject.toJSONBytes(body);


        String result =  HttpUtils.post(Constants.BITCOIN_RPC_URL,header,bodyBytes);
        return result;
    }
}
